Amazon DynamoDB is a key-value and document database that delivers single-digit millisecond performance at any scale. It's a fully managed, multi-region, multi-active, durable database with built-in security, backup and restores, and in-memory caching for internet-scale applications. DynamoDB can handle more than 10 trillion requests per day and can support peaks of more than 20 million requests per second.
Many of the world's fastest-growing businesses such as Lyft, Airbnb, and Redfin as well as enterprises such as Samsung, Toyota, and Capital One depend on the scale and performance of DynamoDB to support their mission-critical workloads.
Hundreds of thousands of AWS customers have chosen DynamoDB as their key-value and document database for mobile, web, gaming, ad tech, IoT, and other applications that need low-latency data access at any scale. Create a new table for your application and let DynamoDB handle the rest.
In DynamoDB, tables, items, and attributes are the core components that you work with. AĀ tableĀ is a collection ofĀ items, and each item is a collection ofĀ attributes. DynamoDB uses primary keys to uniquely identify each item in a table and secondary indexes to provide more querying flexibility. You can use DynamoDB Streams to capture data modification events in DynamoDB tables.
Topics
- Tables, Items, and Attributes
- Primary Key
- Secondary Indexes
- DynamoDB Streams
Tables, Items, and Attributes
The following are the basic DynamoDB components:
- TablesĀ ā Similar to other database systems, DynamoDB stores data in tables. AĀ tableĀ is a collection of data. For example, see the example table calledĀ PeopleĀ that you could use to store personal contact information about friends, family, or anyone else of interest. You could also have aĀ CarsĀ table to store information about vehicles that people drive.
- ItemsĀ ā Each table contains zero or more items. AnĀ itemĀ is a group of attributes that is uniquely identifiable among all of the other items. In aĀ PeopleĀ table, each item represents a person. For aĀ CarsĀ table, each item represents one vehicle. Items in DynamoDB are similar in many ways to rows, records, or tuples in other database systems. In DynamoDB, there is no limit to the number of items you can store in a table.
- AttributesĀ ā Each item is composed of one or more attributes. AnĀ attributeĀ is a fundamental data element, something that does not need to be broken down any further. For example, an item in aĀ PeopleĀ table contains attributes calledĀ PersonID,Ā LastName,Ā FirstName, and so on. For aĀ DepartmentĀ table, an item might have attributes such asĀ DepartmentID,Ā Name,Ā Manager, and so on. Attributes in DynamoDB are similar in many ways to fields or columns in other database systems.
The following diagram shows a table namedĀ PeopleĀ with some example items and attributes.
Note the following about theĀ PeopleĀ table:
- Each item in the table has a unique identifier, or primary key, that distinguishes the item from all of the others in the table. In theĀ PeopleĀ table, the primary key consists of one attribute (PersonID).
- Other than the primary key, theĀ PeopleĀ table is schemaless, which means that neither the attributes nor their data types need to be defined beforehand. Each item can have its distinct attributes.
- Most of the attributes areĀ scalar, which means that they can have only one value. Strings and numbers are common examples of scalars.
- Some of the items have a nested attribute (Address). DynamoDB supports nested attributes up to 32 levels deep.
The following is another example table namedĀ MusicĀ that you could use to keep track of your music collection.
Note the following about theĀ MusicĀ table:
- The primary key forĀ MusicĀ consists of two attributes (ArtistĀ andĀ song title). Each item in the table must have these two attributes. The combination ofĀ ArtistĀ andĀ SongTitleĀ distinguishes each item in the table from all of the others.
- Other than the primary key, theĀ MusicĀ table is schemaless, which means that neither the attributes nor their data types need to be defined beforehand. Each item can have its distinct attributes.
- One of the items has a nested attribute (PromotionInfo), which contains other nested attributes. DynamoDB supports nested attributes up to 32 levels deep.
Primary Key
When you create a table, in addition to the table name, you must specify the primary key of the table. The primary key uniquely identifies each item in the table, so that no two items can have the same key.
DynamoDB supports two different kinds of primary keys:
- Partition keyĀ ā A simple primary key, composed of one attribute known as theĀ partition key.
DynamoDB uses the partition key's value as input to an internal hash function. The output from the hash function determines the partition (physical storage internal to DynamoDB) in which the item will be stored.
In a table that has only a partition key, no two items can have the same partition key value.
TheĀ PeopleĀ table described inĀ Tables, Items, and AttributesĀ is an example of a table with a simple primary key (PersonID). You can access any item in theĀ PeopleĀ table directly by providing theĀ PersonIdĀ value for that item.
- Partition key and sort keyĀ ā Referred to as aĀ composite primary key, this type of key are composed of two attributes. The first attribute is theĀ partition key, and the second attribute is theĀ sort key.
DynamoDB uses the partition key value as input to an internal hash function. The output from the hash function determines the partition (physical storage internal to DynamoDB) in which the item will be stored. All items with the same partition key value are stored together, in sorted order by sort key value.
In a table that has a partition key and a sort key, two items can have the same partition key value. However, those two items must have different sort key values.
TheĀ MusicĀ table described in Tables, Items, and Attributes is an example of a table with a composite primary key (ArtistĀ andĀ song title). You can access any item in theĀ MusicĀ table directly if you provide theĀ ArtistĀ andĀ SongTitleĀ values for that item.
A composite primary key gives you additional flexibility when querying data. For example, if you provide only the value forĀ Artist, DynamoDB retrieves all of the songs by that artist. To retrieve only a subset of songs by a particular artist, you can provide a value forĀ ArtistĀ along with a range of values forĀ SongTitle.
Secondary Indexes
You can create one or more secondary indexes on a table. AĀ secondary indexĀ lets you query the data in the table using an alternate key, in addition to queries against the primary key. DynamoDB doesn't require that you use indexes, but they give your applications more flexibility when querying your data. After you create a secondary index on a table, you can read data from the index in much the same way as you do from the table.
DynamoDB supports two kinds of indexes:
- Global secondary index ā An index with a partition key and sort key that can be different from those on the table.
- Local secondary index ā An index that has the same partition key as the table, but a different sort of key.
Each table in DynamoDB has a quota of 20 global secondary indexes (default quota) and 5 local secondary indexes per table.
In the exampleĀ MusicĀ table shown previously, you can query data items byĀ ArtistĀ (partition key) or byĀ ArtistĀ andĀ SongTitleĀ (partition key and sort key). What if you also wanted to query the data byĀ GenreĀ andĀ AlbumTitle? To do this, you could create an index onĀ GenreĀ andĀ album title, and then query the index in much the same way as you'd query theĀ MusicĀ table.
The following diagram shows the exampleĀ MusicĀ table, with a new index calledĀ GenreAlbumTitle. In the index,Ā GenreĀ is the partition key andĀ AlbumTitleĀ is the sort key.
Note the following about theĀ GenreAlbumTitleĀ index:
- Every index belongs to a table, which is called theĀ base tableĀ for the index. In the preceding example,Ā MusicĀ is the base table for theĀ GenreAlbumTitleĀ index.
- DynamoDB maintains indexes automatically. When you add, update, or delete an item in the base table, DynamoDB adds, updates, or deletes the corresponding item in any indexes that belong to that table.
- When you create an index, you specify which attributes will be copied, orĀ projected, from the base table to the index. At a minimum, DynamoDB projects the key attributes from the base table into the index. This is the case with GenreAlbumTitle, where only the key attributes from the Music table are projected into the index.
You can query theĀ GenreAlbumTitleĀ index to find all albums of a particular genre (for example, allĀ RockĀ albums). You can also query the index to find all albums within a particular genre that have certain album titles (for example, allĀ CountryĀ albums with titles that start with the letter H).
DynamoDB Streams
DynamoDB Streams is an optional feature that captures data modification events in DynamoDB tables. The data about these events appear in the stream in near-real-time, and in the order that the events occurred.
Each event is represented by aĀ stream record. If you enable a stream on a table, DynamoDB Streams writes a stream record whenever one of the following events occurs:
- A new item is added to the table: The stream captures an image of the entire item, including all of its attributes.
- An item is updated: The stream captures the "before" and "after" images of any attributes that were modified in the item.
- An item is deleted from the table: The stream captures an image of the entire item before it was deleted.
Each stream record also contains the name of the table, the event timestamp, and other metadata. Stream records have a lifetime of 24 hours; after that, they are automatically removed from the stream.
You can use DynamoDB Streams together with AWS Lambda to create aĀ triggerācode that runs automatically whenever an event of interest appears in a stream. For example, consider aĀ CustomerĀ table that contains customer information for a company. Suppose that you want to send a "welcome" email to each new customer. You could enable a stream on that table, and then associate the stream with a Lambda function. The Lambda function would run whenever a new stream record appears, but only process new items added to theĀ CustomersĀ table. For any item that has an EmailAddress attribute, the Lambda function would invoke Amazon Simple Email Service (Amazon SES) to send an email to that address.
Note
In this example, the last customer, Craig Roe, will not receive an email because he doesn't have an EmailAddress.
In addition to triggers, DynamoDB Streams enables powerful solutions such as data replication within and across AWS Regions, materialized views of data in DynamoDB tables, data analysis using Kinesis materialized views, and much more.
Benefits
Performance at scale
DynamoDB supports some of the worldās largest scale applications by providing consistent, single-digit millisecond response times at any scale. You can build applications with virtually unlimited throughput and storage. DynamoDB global tables replicate your data across multiple AWS Regions to give you fast, local access to data for your globally distributed applications. For use cases that require even faster access with microsecond latency, DynamoDB Accelerator (DAX) provides a fully managed in-memory cache.
No servers to manage
DynamoDB is serverless with no servers to provision, patch, or manage and no software to install, maintain or operate. DynamoDB automatically scales tables up and down to adjust for capacity and maintain performance. Availability and fault tolerance are built-in, eliminating the need to architect your applications for these capabilities. DynamoDB provides both provisioned and on-demand capacity modes so that you can optimize costs by specifying capacity per workload or paying for only the resources you consume.
Enterprise-ready
DynamoDB supports ACID transactions to enable you to build business-critical applications at scale. DynamoDB encrypts all data by default and provides fine-grained identity and access control on all your tables. You can create full backups of hundreds of terabytes of data instantly with no performance impact to your tables, and recover to any point in time in the preceding 35 days with no downtime. You also can export your DynamoDB table data to your data lake in Amazon S3 to perform analytics at any scale. DynamoDB is also backed by a service level agreement for guaranteed availability.
Applications
Serverless Web Apps
Build powerful web applications that automatically scale up and down. You don't need to maintain servers, and your applications have automated high availability.
Mobile Backends
Use DynamoDB and AWS AppSync to build interactive mobile and web apps with real-time updates, offline data access, and data sync with built-in conflict resolution.
Microservices
Build flexible and reusable microservices using DynamoDB as a serverless data store for consistent and fast performance.
Use cases
Ad Tech
Companies in the advertising technology (ad tech) vertical use DynamoDB as a key-value store for storing various kinds of marketing data, such as user profiles, user events, clicks, and visited links. Applicable use cases include real-time bidding (RTB), ad targeting, and attribution. These use cases require a high request rate (millions of requests per second), low predictable latency, and reliability. Companies use caching through DynamoDB Accelerator (DAX) when they have high read volumes or need submillisecond read latency. Increasingly, ad tech companies need to deploy their RTB and ad targeting platforms in more than one geographical AWS Region, which requires data replication between Regions.
Gaming
Companies in the gaming vertical use DynamoDB in all capabilities of game platforms, including game state, player data, session history, and leaderboards. The main benefits that these companies get from DynamoDB are its ability to scale reliably to millions of concurrent users and requests while ensuring consistently low latency measured in single-digit milliseconds. Also, as a fully managed service, DynamoDB has no operational overhead, so game developers can focus on developing their games instead of managing databases. Also, as game developers are increasingly looking to expand from a single AWS Region to multiple AWS Regions, they can rely on DynamoDB global tables for multiple-Region, active-active replication of data.
Common use cases:
- Ā· User profile stores in RTB and ad targeting
- Ā· User events, clickstreams, and impressions datastore
- Ā· Metadata stores for assets
- Ā· Popular-item caches
- Ā· Game states
- Ā· Player data stores
- Ā· Player session history data stores
- Ā· Leaderboards
The PokƩmon Company migrated global configuration and time-to-live (TTL) data to Amazon DynamoDB, resulting in a 90 percent reduction in bot login attempts.
Retail
Many companies in the retail space use common DynamoDB design patterns to deliver consistently low latency for mission-critical use cases. Being free from scaling concerns and the operational burden is a key competitive advantage and an enabler for high-velocity, extreme-scaled events such as Amazon Prime Day, whose magnitudes are difficult to forecast. Scaling up and down allows these customers to pay only for the capacity they need and keeps precious technical resources focused on innovations rather than operations.
Banking and Finance
As companies in banking and finance build more cloud-native applications, they seek to use fully managed services to increase agility, reduce time to market, and minimize operational overhead. At the same time, they have to ensure the security, reliability, and high availability of their applications. As these companies expand their existing services that are backed by legacy mainframe systems, they find that legacy systems are unable to meet the scalability demands of their growing user base, new platforms such as mobile applications, and the resulting increases in traffic. To solve this problem, they replicate data from their mainframes to the cloud to offload the traffic.
Ready to level up your skills?
Enroll in our AWS DynamoDB Certification Course and get hands-on with real-world projects!
Author Details
Vaibhav Umarvaishya
Cloud Engineer | Solution Architect
As a Cloud Engineer and AWS Solutions Architect Associate at NovelVista, I specialized in designing and deploying scalable and fault-tolerant systems on AWS. My responsibilities included selecting suitable AWS services based on specific requirements, managing AWS costs, and implementing best practices for security. I also played a pivotal role in migrating complex applications to AWS and advising on architectural decisions to optimize cloud deployments.
Confused About Certification?
Get Free Consultation Call